package com.sxf.djiuav;

import androidx.lifecycle.MutableLiveData;
import dji.sdk.keyvalue.key.FlightControllerKey;
import dji.sdk.keyvalue.key.KeyTools;
import dji.sdk.keyvalue.value.common.DoubleRect;
import dji.sdk.keyvalue.value.common.LocationCoordinate2D;
import dji.sdk.keyvalue.value.common.LocationCoordinate3D;
import dji.sdk.keyvalue.value.flightcontroller.FlyToMode;
import dji.v5.common.callback.CommonCallbacks;
import dji.v5.common.error.IDJIError;
import dji.v5.manager.KeyManager;
import dji.v5.manager.intelligent.AutoSensingInfo;
import dji.v5.manager.intelligent.AutoSensingInfoListener;
import dji.v5.manager.intelligent.AutoSensingTarget;
import dji.v5.manager.intelligent.IMissionCapabilityListener;
import dji.v5.manager.intelligent.IMissionInfoListener;
import dji.v5.manager.intelligent.IntelligentFlightInfo;
import dji.v5.manager.intelligent.IntelligentFlightInfoListener;
import dji.v5.manager.intelligent.IntelligentFlightManager;
import dji.v5.manager.intelligent.IntelligentModel;
import dji.v5.manager.intelligent.TargetType;
import dji.v5.manager.intelligent.flyto.FlyToInfo;
import dji.v5.manager.intelligent.flyto.FlyToParam;
import dji.v5.manager.intelligent.flyto.FlyToTarget;
import dji.v5.manager.intelligent.poi.POICapability;
import dji.v5.manager.intelligent.poi.POIInfo;
import dji.v5.manager.intelligent.poi.POIParam;
import dji.v5.manager.intelligent.poi.POITarget;
import dji.v5.manager.intelligent.smarttrack.SmartTrackInfo;
import dji.v5.manager.intelligent.smarttrack.SmartTrackTarget;
import dji.v5.manager.intelligent.spotlight.SpotLightInfo;
import dji.v5.manager.intelligent.spotlight.SpotLightTarget;

import java.util.List;

public class IntelligentFlight {

    public final MutableLiveData<Double> aircraftHeight = new MutableLiveData<>();
    public final MutableLiveData<LocationCoordinate2D> aircraftLocation = new MutableLiveData<>();

    public final MutableLiveData<IntelligentFlightInfo> intelligentFlightInfo = new MutableLiveData<>();
    public final MutableLiveData<AutoSensingInfo> autoSensingInfo = new MutableLiveData<>();
    public final MutableLiveData<List<IntelligentModel>> intelligentModels = new MutableLiveData<>();
    public final MutableLiveData<Integer> runningModelIndex = new MutableLiveData<>();

    public final MutableLiveData<POIInfo> poiInfo = new MutableLiveData<>();
    public final MutableLiveData<POITarget> poiTarget = new MutableLiveData<>();

    public final MutableLiveData<FlyToInfo> flyToInfo = new MutableLiveData<>();
    public final MutableLiveData<FlyToTarget> flyToTarget = new MutableLiveData<>();

    public final MutableLiveData<SpotLightInfo> spotLightInfo = new MutableLiveData<>();
    public final MutableLiveData<SpotLightTarget> spotLightTarget = new MutableLiveData<>();

    public final MutableLiveData<SmartTrackInfo> smartTrackInfo = new MutableLiveData<>();
    public final MutableLiveData<SmartTrackTarget> smartTrackTarget = new MutableLiveData<>();

    private DJIApplication app;
    public IntelligentFlight(DJIApplication app) {
        this.app = app;
    }

    private final IntelligentFlightInfoListener intelligentFlightInfoListener = new IntelligentFlightInfoListener() {
        @Override
        public void onIntelligentFlightInfoUpdate(IntelligentFlightInfo info) {
            intelligentFlightInfo.postValue(info);
        }

        @Override
        public void onIntelligentFlightErrorUpdate(IDJIError error) {
            // Empty implementation
        }
    };

    private final AutoSensingInfoListener autoSensingInfoListener = new AutoSensingInfoListener() {
        @Override
        public void onAutoSensingInfoUpdate(AutoSensingInfo info) {
            autoSensingInfo.postValue(info);
        }

        @Override
        public void onTrackingTargetUpdate(AutoSensingTarget target) {
            // Empty implementation
        }

        @Override
        public void onIntelligentModelUpdate(List<IntelligentModel> models) {
            intelligentModels.postValue(models);
        }

        @Override
        public void onRunningIntelligentModelUpdate(int modelId) {
            runningModelIndex.postValue(modelId);
        }
    };

    private final IMissionInfoListener<POIInfo, POITarget> poiInfoListener = new IMissionInfoListener<POIInfo, POITarget>() {
        @Override
        public void onMissionInfoUpdate(POIInfo info) {
            poiInfo.postValue(info);
        }

        @Override
        public void onMissionTargetUpdate(POITarget target) {
            poiTarget.postValue(target);
        }
    };

    private final IMissionCapabilityListener<POICapability> poiCapabilityListener = new IMissionCapabilityListener<POICapability>() {
        @Override
        public void onMissionCapabilityUpdate(POICapability capability) {
            // Empty implementation
        }
    };

    private final IMissionInfoListener<FlyToInfo, FlyToTarget> flyToInfoListener = new IMissionInfoListener<FlyToInfo, FlyToTarget>() {
        @Override
        public void onMissionInfoUpdate(FlyToInfo info) {
            flyToInfo.postValue(info);
        }

        @Override
        public void onMissionTargetUpdate(FlyToTarget target) {
            flyToTarget.postValue(target);
        }
    };

    private final IMissionInfoListener<SpotLightInfo, SpotLightTarget> spotLightInfoListener = new IMissionInfoListener<SpotLightInfo, SpotLightTarget>() {
        @Override
        public void onMissionInfoUpdate(SpotLightInfo info) {
            spotLightInfo.postValue(info);
        }

        @Override
        public void onMissionTargetUpdate(SpotLightTarget target) {
            spotLightTarget.postValue(target);
        }
    };

    private final IMissionInfoListener<SmartTrackInfo, SmartTrackTarget> smartTrackListener = new IMissionInfoListener<SmartTrackInfo, SmartTrackTarget>() {
        @Override
        public void onMissionInfoUpdate(SmartTrackInfo info) {
            smartTrackInfo.postValue(info);
        }

        @Override
        public void onMissionTargetUpdate(SmartTrackTarget target) {
            smartTrackTarget.postValue(target);
        }
    };

    public void initListener() {
        IntelligentFlightManager.getInstance().addIntelligentFlightInfoListener(intelligentFlightInfoListener);
        IntelligentFlightManager.getInstance().addAutoSensingInfoListener(autoSensingInfoListener);

        // 使用正确的方法名来获取各个任务管理器
        IntelligentFlightManager.getInstance().getPOIMissionManager().addMissionInfoListener(poiInfoListener);
        IntelligentFlightManager.getInstance().getPOIMissionManager().addMissionCapabilityListener(poiCapabilityListener);
        IntelligentFlightManager.getInstance().getFlyToMissionManager().addMissionInfoListener(flyToInfoListener);
        IntelligentFlightManager.getInstance().getSpotLightManager().addMissionInfoListener(spotLightInfoListener);
        IntelligentFlightManager.getInstance().getSmartTrackMissionManager().addMissionInfoListener(smartTrackListener);

        KeyManager.getInstance().listen(KeyTools.createKey(FlightControllerKey.KeyAltitude), this, new CommonCallbacks.KeyListener<Double>() {
            @Override
            public void onValueChange(Double oldValue, Double newValue) {
                aircraftHeight.postValue(newValue);
            }
        });

        KeyManager.getInstance().listen(KeyTools.createKey(FlightControllerKey.KeyAircraftLocation), this, new CommonCallbacks.KeyListener<LocationCoordinate2D>() {
            @Override
            public void onValueChange(LocationCoordinate2D oldValue, LocationCoordinate2D newValue) {
                aircraftLocation.postValue(newValue);
            }
        });
    }

    public void cleanListener() {
        IntelligentFlightManager.getInstance().removeIntelligentFlightInfoListener(intelligentFlightInfoListener);
        IntelligentFlightManager.getInstance().removeAutoSensingInfoListener(autoSensingInfoListener);

        // 使用正确的方法名来获取各个任务管理器
        IntelligentFlightManager.getInstance().getPOIMissionManager().removeMissionInfoListener(poiInfoListener);
        IntelligentFlightManager.getInstance().getPOIMissionManager().removeMissionCapabilityListener(poiCapabilityListener);
        IntelligentFlightManager.getInstance().getFlyToMissionManager().removeMissionInfoListener(flyToInfoListener);
        IntelligentFlightManager.getInstance().getSpotLightManager().removeMissionInfoListener(spotLightInfoListener);
        IntelligentFlightManager.getInstance().getSmartTrackMissionManager().removeMissionInfoListener(smartTrackListener);

        KeyManager.getInstance().cancelListen(this);
    }

    public void startAutoSensing() {
        IntelligentFlightManager.getInstance().startAutoSensing(new CommonCallbacks.CompletionCallback() {
            @Override
            public void onSuccess() {
                app.addLog("开始自动感知成功");
            }

            @Override
            public void onFailure(IDJIError error) {
                app.addLog("开始自动感知错误: " + (error != null ? error.description() : "未知错误"));
            }
        });
    }

    public void stopAutoSensing() {
        IntelligentFlightManager.getInstance().stopAutoSensing(new CommonCallbacks.CompletionCallback() {
            @Override
            public void onSuccess() {
                app.addLog("停止自动感知成功");
            }

            @Override
            public void onFailure(IDJIError error) {
                app.addLog("停止自动感知错误: " + (error != null ? error.description() : "未知错误"));
            }
        });
    }

    public void selectIntelligentModel(int model) {
        IntelligentFlightManager.getInstance().selectIntelligentModel(model, new CommonCallbacks.CompletionCallback() {
            @Override
            public void onSuccess() {
                app.addLog("选择智能模式成功: " + model);
            }

            @Override
            public void onFailure(IDJIError error) {
                app.addLog("选择智能模式错误: " + (error != null ? error.description() : "未知错误"));
            }
        });
    }

    public void startPOIMission(POITarget target) {
        IntelligentFlightManager.getInstance().getPOIMissionManager().startMission(target, null,
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog("开始POI任务成功");
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog("开始POI任务错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void stopPOIMission() {
        IntelligentFlightManager.getInstance().getPOIMissionManager().stopMission(
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog("停止POI任务成功");
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog("停止POI任务错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void updatePOIMissionTarget(POITarget target) {
        IntelligentFlightManager.getInstance().getPOIMissionManager().updateMissionTarget(target,
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog("更新POI目标成功");
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog("更新POI目标错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void updatePOICircleSpeed(double speed) {
        POIParam param = new POIParam();
        param.setCircleSpeed(speed);
        IntelligentFlightManager.getInstance().getPOIMissionManager().updateMissionParam(param,
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog("更新POI环绕速度成功: " + speed);
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog("更新POI环绕速度错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void lockCircularVelocity(boolean lock) {
        IntelligentFlightManager.getInstance().getPOIMissionManager().lockCircularVelocity(
                lock,
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog((lock ? "锁定" : "解锁") + "环绕速度成功");
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog((lock ? "锁定" : "解锁") + "环绕速度错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void lockGimbalPitch(boolean lock) {
        IntelligentFlightManager.getInstance().getPOIMissionManager().lockGimbalPitch(
                lock,
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog((lock ? "锁定" : "解锁") + "云台俯仰成功");
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog((lock ? "锁定" : "解锁") + "云台俯仰错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void startFlyTo(double lon, double lat, double alt) {
        FlyToTarget target = new FlyToTarget();
        LocationCoordinate3D lc = new LocationCoordinate3D(lat, lon, alt);
        String log = "指点飞行,经:" + lc.getLongitude() + ",纬:" + lc.getLatitude() + ",高:" + lc.getAltitude();
        app.addLog(log);
        target.setTargetLocation(lc);
        startFlyTo(target);
    }

    public void startFlyTo(FlyToTarget target) {
        IntelligentFlightManager.getInstance().getFlyToMissionManager().startMission(target, null,
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog("开始指点飞行成功");
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog("开始指点飞行错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void stopFlyTo() {
        IntelligentFlightManager.getInstance().getFlyToMissionManager().stopMission(
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog("停止指点飞行成功");
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog("停止指点飞行错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void setFlyToMode(FlyToMode mode) {
        FlyToParam param = new FlyToParam();
        param.setFlyToMode(mode);
        IntelligentFlightManager.getInstance().getFlyToMissionManager().updateMissionParam(param,
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog("设置飞行模式成功: " + mode);
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog("设置飞行模式错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void setFlyToHeight(int height) {
        FlyToParam param = new FlyToParam();
        param.setHeight(height);
        IntelligentFlightManager.getInstance().getFlyToMissionManager().updateMissionParam(param,
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog("设置飞行高度成功: " + height);
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog("设置飞行高度错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void enterSpotLightMode() {
        IntelligentFlightManager.getInstance().getSpotLightManager().enterSpotLightMode(
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog("进入聚光灯模式成功");
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog("进入聚光灯模式错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void exitSpotLightMode() {
        IntelligentFlightManager.getInstance().getSpotLightManager().exitSpotLightMode(
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog("退出聚光灯模式成功");
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog("退出聚光灯模式错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void startSpotlight() {
        IntelligentFlightManager.getInstance().getSpotLightManager().startMission(null, null,
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog("开始聚光灯任务成功");
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog("开始聚光灯任务错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void selectAutoTarget(int index) {
        SpotLightTarget param = new SpotLightTarget();
        param.setTargetIndex(index);
        param.setTargetType(TargetType.INDEX);
        IntelligentFlightManager.getInstance().getSpotLightManager().updateMissionTarget(param,
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog("选择自动目标成功: " + index);
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog("选择自动目标错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void selectManualTarget(DoubleRect bound) {
        SpotLightTarget param = new SpotLightTarget();
        param.setTargetRect(bound);
        param.setTargetType(TargetType.RECT);
        IntelligentFlightManager.getInstance().getSpotLightManager().updateMissionTarget(param,
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog("选择手动目标成功");
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog("选择手动目标错误: " + (error != null ? error.description() : "未知错误"));
                    }
                });
    }

    public void confirmTarget() {
        IntelligentFlightManager.getInstance().getSpotLightManager().confirmTarget(new CommonCallbacks.CompletionCallback() {
            @Override
            public void onSuccess() {
                app.addLog("确认目标成功");
            }

            @Override
            public void onFailure(IDJIError error) {
                app.addLog("确认目标错误: " + (error != null ? error.description() : "未知错误"));
            }
        });
    }

    public void stopSpotlight() {
        IntelligentFlightManager.getInstance().getSpotLightManager().stopMission(new CommonCallbacks.CompletionCallback() {
            @Override
            public void onSuccess() {
                app.addLog("停止聚光灯任务成功");
            }

            @Override
            public void onFailure(IDJIError error) {
                app.addLog("停止聚光灯任务错误: " + (error != null ? error.description() : "未知错误"));
            }
        });
    }

    public void startSmartTrack() {
        IntelligentFlightManager.getInstance().getSmartTrackMissionManager().startMission(null, null, new CommonCallbacks.CompletionCallback() {
            @Override
            public void onSuccess() {
                app.addLog("开始智能跟踪成功");
            }

            @Override
            public void onFailure(IDJIError error) {
                app.addLog("开始智能跟踪错误: " + (error != null ? error.description() : "未知错误"));
            }
        });
    }

    public void stopSmartTrack() {
        IntelligentFlightManager.getInstance().getSmartTrackMissionManager().stopMission(new CommonCallbacks.CompletionCallback() {
            @Override
            public void onSuccess() {
                app.addLog("停止智能跟踪成功");
            }

            @Override
            public void onFailure(IDJIError error) {
                app.addLog("停止智能跟踪错误: " + (error != null ? error.description() : "未知错误"));
            }
        });
    }

    public void selectTrackingTarget(int index) {
        SmartTrackTarget param = new SmartTrackTarget();
        param.setIndex(index);
        IntelligentFlightManager.getInstance().getSmartTrackMissionManager().updateMissionTarget(param,
                new CommonCallbacks.CompletionCallback() {
                    @Override
                    public void onSuccess() {
                        app.addLog("选择跟踪目标成功: " + index);
                    }

                    @Override
                    public void onFailure(IDJIError error) {
                        app.addLog("选择跟踪目标错误: " + index + "," + (error != null ? error.description() : "未知错误"));
                    }
                });
    }
}